app.controller("main",['$scope','$window','service','$rootScope','toast',function($scope,$window,service,$rootScope,toast){
    $scope.viewBag={};
    function success(data){
        console.log(data);
        if(data.code==2){
            // alert(data.msg);
            // $window.location.href="index.html";
            toast(data.msg,function(){
               $window.location.href="index.html";
            })
        }
        $scope.viewBag=data;  
    }
    service.get($scope.host+"/Main/index",'',success)
    $scope.removeMask=function(){
        $rootScope.loadStatus=false;
    }
    $scope.$on("$stateChangeSuccess",function(evt,toState,roParams,fromState){
        $scope.navStatus=toState.name.split(".");
		console.log($scope.navStatus);
    }) 
}])
app.controller("index",['$scope','service',function($scope,service){
    $scope.loadStatus=false;
    function success(data){
        console.log(data);
        $scope.data=data;
    }
    service.get($scope.host+"/Home/index",'',success)  
}])
app.controller("notice",['$scope',function($scope){
    
}])
app.controller("broadcast",['$scope','service',function($scope,service){
    function success(data){
        console.log(data);
        $scope.data=data;
        $scope.maxSize = 4;
        $scope.totalItems = data.recordCount;
        // $scope.totalItems=100;
        // $scope.currentPage = 1;
        $scope.typeId=data.typeId
    }
    $scope.pageChange=function(){
        console.log($scope.currentPage);
        service.get($scope.host+"/infoCenter/notice/"+$scope.typeId+"/"+$scope.currentPage,'',function(data){
            console.log(data);
            $scope.data=data;
        })
    }
    service.get($scope.host+"/infoCenter/notice",'',success)
    $scope.getNotice=function(typeId){
        $scope.typeId=typeId;
        service.get($scope.host+"/infoCenter/notice/"+typeId,'',function(data){
            $scope.data=data;
            $scope.totalItems = data.recordCount;
        }) 
    }
}])
app.controller("noticeDetail",['$scope','service','$sce','$stateParams',function($scope,service,$sce,$stateParams){
    function success(data){
         $scope.data=data.data;
        //  console.log($scope.data);
    }
    service.get($scope.host+"/infoCenter/noticeDetail/"+$stateParams.id,'',success)
     //转义html
    $scope.raw = function (str) {
        return $sce.trustAsHtml(str);
    }; 
}])
app.controller("sendEmail",['$scope','service','toast',function($scope,service,toast){
    
    service.get($scope.host+"/InfoCenter/mail",'',function(data){
        $scope.data=data;
        $scope.students=$scope.data.students;
        var obj={"name":"管理员","id":0};
        $scope.students.unshift(obj);
    })
   $scope.sendEmail=function(){
       if(!document.querySelector("input[name='targetId']").value){
            toast("收件人不能为空！");
           return;
       }
       if($scope.sendMail.title.$error.required){
           toast("邮件标题不能为空！");
           return;
       }
       service.post("form",$scope.host+"/InfoCenter/mailSend",function(data){
            if(data.code==0){
                toast(data.msg);
            }
       })
   }
   
}])
app.controller("receiveBox",['$scope','service','toast','$timeout','$state',function($scope,service,toast,$timeout,$state){
    $scope.emailChecked=false;
    $scope.unReadStatus=false;
    $scope.readStatus=false;
    $scope.selectAll=function(){
        $scope.emailChecked=!$scope.emailChecked;
    }
    $scope.selectUnread=function(){
        if(!$scope.unReadStatus){
            $scope.mailsData=[];
            angular.forEach($scope.data.mails,function(data){
                console.log(data);
                if(!data.readed){
                    $scope.mailsData.push(data);
                }
            })
        }else{
            $scope.mailsData=$scope.data.mails;
        }
        $scope.unReadStatus=!$scope.unReadStatus;
    }
    $scope.selectRead=function(){
        if(!$scope.readStatus){
            $scope.mailsData=[];
            angular.forEach($scope.data.mails,function(data){
                console.log(data);
                if(data.readed){
                    $scope.mailsData.push(data);
                }
            })
        }else{
             $scope.mailsData=$scope.data.mails;
        }
        $scope.readStatus=!$scope.readStatus;
    }
    service.get($scope.host+"/InfoCenter/mail/0/1",'',function(data){
         console.log(data);
         $scope.data=data;
         $scope.mailsData=data.mails;
         $scope.maxSize = 4;
         $scope.totalItems = data.recordCount;
       
    })
    $scope.pageChange=function(){
        service.get($scope.host+"/InfoCenter/mail/0/"+$scope.currentPage,'',function(data){
            $scope.data=data;
        })
    }
   
    str="";
    $scope.delMail=function(id,$event){
        var el=document.querySelectorAll("input:checked")
        if(!el.length){
            toast("没有选中任何邮件，请重新选择！")
        }else{
            angular.forEach(el,function(data,index){
                  var value=angular.element(data).val();
                  console.log(data);
                  console.log(index);
               
                if(index==0){
                    console.log(index)
                    str+=value;
                }else{
                    str+=(","+value)
                }
            })
        }
        service.get($scope.host+"/infoCenter/delMail/0/"+str,'',function(data){
            toast(data.msg);
            $timeout(function(){
                $state.reload();
            },1000)
        })
   }
}])
app.controller("sendBox",['$scope','service','toast','$timeout','$state',function($scope,service,toast,$timeout,$state){
    $scope.emailChecked=false;
    $scope.unReadStatus=false;
    $scope.readStatus=false;
    $scope.selectAll=function(){
        $scope.emailChecked=!$scope.emailChecked;
    }
    $scope.selectUnread=function(){
        if(!$scope.unReadStatus){
            $scope.mailsData=[];
            angular.forEach($scope.data.mails,function(data){
                console.log(data);
                if(!data.readed){
                    $scope.mailsData.push(data);
                }
            })
        }else{
            $scope.mailsData=$scope.data.mails;
        }
        $scope.unReadStatus=!$scope.unReadStatus;
    }
    $scope.selectRead=function(){
        if(!$scope.readStatus){
            $scope.mailsData=[];
            angular.forEach($scope.data.mails,function(data){
                console.log(data);
                if(data.readed){
                    $scope.mailsData.push(data);
                }
            })
        }else{
             $scope.mailsData=$scope.data.mails;
        }
        $scope.readStatus=!$scope.readStatus;
    }
    service.get($scope.host+"/InfoCenter/mail/1/1",'',function(data){
         $scope.data=data;
         $scope.mailsData=data.mails;
         $scope.maxSize = 4;
         $scope.totalItems = data.recordCount;
    })
    console.log("sendBox")
    $scope.pageChange=function(){
        service.get($scope.host+"/InfoCenter/mail/0/"+$scope.currentPage,'',function(data){
            console.log(data);
            $scope.data=data;
        })
    }
    str="";
    $scope.delMail=function(id,$event){
        var el=document.querySelectorAll("input:checked")
        if(!el.length){
            toast("没有选中任何邮件，请重新选择！")
        }else{
            angular.forEach(el,function(data,index){
                  var value=angular.element(data).val();
                  console.log(data);
                  console.log(index);
               
                if(index==0){
                    console.log(index)
                    str+=value;
                }else{
                    str+=(","+value)
                } 
            })
        }
        service.get($scope.host+"/infoCenter/delMail/0/"+str,'',function(data){
            toast(data.msg);
            $timeout(function(){
                $state.reload();
            },1000)
        })
   }
}])
app.controller("emailDetail",['$scope','service','$sce','$stateParams','toast',function($scope,service,$sce,$stateParams,toast){
    service.get($scope.host+"/InfoCenter/mailDetail/"+$stateParams.id,'',function(data){
         console.log(data);
         $scope.data=data;
         //删除具体某封邮件
         $scope.deleteEmail=function(id){
            //  console.log($scope.data.id);
            if($index>=0){
                if(comfirm("是否删除该邮件")){
                    $scope.data.data.splice()
                }
            }
         }
         
    })
    $scope.raw = function (str) {
        return $sce.trustAsHtml(str);
    }
    $scope.postForm=function(){
        if($scope.replyBox.content.$error.required){
            toast("邮件内容不能为空，请重新编辑！")
            return;
        }
       service.post("form",$scope.host+"/InfoCenter/mailSend",function(data){
           console.log(data);
       })
    }
}])
//个人中心控制器
app.controller("userInfo",['$scope','$state',function($scope,$state){
    $scope.items=
        [{"item":"选择课程","url":"teachPlan.courseSelect"},
        {"item":"成绩组成","url":"teachPlan.scoreComponent"},
        {"item":"考试安排","url":"teachPlan.examArrange"}]

}])
//修改密码 控制器
app.controller("password",['$scope','service','toast',function($scope,service,toast){
    $scope.postForm=function(){
        service.post("form",$scope.host+"/UserInfo/modifyPwd",function(data){
            if(data.code==0){
               toast(data.msg);
            }else{
                toast(data.msg);
            }
        })
    }
}])
//成绩查询控制器
app.controller("score",['$scope','service',function($scope,service){  
    service.get($scope.host+"/userInfo/index/0",'',function(data){
        $scope.data=data;
        $scope.termScore=$scope.data.terms[0];
        
    })
    $scope.showScore=function(i){
        $scope.termScore=$scope.data.terms[i]
        console.log($scope.termScore);
    }
}])
app.controller("teachPlan",['$scope','$location','$state',function($scope,$location,$state){
    $scope.items=
        [{"item":"选择课程","url":"teachPlan.courseSelect","branch":"courseSelect"},
        {"item":"成绩组成","url":"teachPlan.scoreComponent","branch":"scoreComponent"},
        {"item":"考试安排","url":"teachPlan.examArrange","branch":"examArrange"}]

}])
app.controller("courseSelect",['$scope','service','toast',function($scope,service,toast){
    service.get($scope.host+"/Plan",'',function(data){
        console.log(data);
        $scope.data=data;
    })
    $scope.showTerm=function(i){
        $scope.data.currTerm=i;
    }
    $scope.postForm=function(){
        service.post("form",$scope.host+"/Plan/select/"+$scope.data.currTerm,function(data){
            if(data.code==0){
                toast(data.msg)
            }else{
                toast(data.msg)
            }
            if(data.terms){
                $scope.data.terms=data.terms; 
                $scope.$apply();
            }
        })
    }
}])
console.log(11);
app.controller("scoreComponent",['$scope','service',function($scope,service){
    service.get($scope.host+"/Plan/score",'',function(data){
        $scope.data=data;
    })
    $scope.showTerm=function(i){
        $scope.data.currTerm=i;
    }
}])
app.controller("examArrange",['$scope','service','toast','$state',function($scope,service,toast,$state){
    $scope.time=new Date().getTime();
    console.log($scope.time);
    service.get($scope.host+"/Plan/examPlan",'',function(data){
        console.log(data);
        $scope.data=data;
    })
    $scope.order=function(examId){
        service.get($scope.host+"/Plan/examOrder/"+examId,'',function(data){
            console.log(data);
            toast(data.msg);
        })
    }

    $scope.startExam=function(id){
        $state.go("examOnline",{id:id})
    }
}])
app.controller("course",['$scope','$location','$state',function($scope,$location,$state,$stateParams){
    $scope.items=
        [{"item":"课程学习","url":"course.courseStudy","branch":"courseStudy"},
        {"item":"课程直播","url":"course.courseLive","branch":"courseLive","subBranch":"liveRoom"},
        {"item":"课程作业","url":"course.coursework","branch":"coursework"},
        {"item":"知识问答","url":"course.question.allQuestion","branch":"question"},
        {"item":"考前辅导","url":"course.examPrepare","branch":"examPrepare"}]
}])
app.controller("courseStudy",['$scope','service',function($scope,service){
    service.get($scope.host+"/Course/index",'',function(data){
        console.log(data);
        $scope.data=data;
    })
    $scope.showTerm=function(i){
          $scope.data.currTerm=i;
    }
}])
app.controller("wareView",['$scope','service','$state','$interval','$http','toast',function($scope,service,$state,$interval,$http,toast){
    var courseId=$state.params.courseId;
    var wareId=$state.params.wareId;
    $scope.courseId=courseId;
    $scope.currentWareId=wareId;
    var timer=null;
    service.get($scope.host+"/Course/wareView/"+courseId+"/"+wareId,'',function(data){
         $scope.data=data;
         console.log(data);
        if(wareId=="0"){
            $state.go("course.wareView",{courseId:courseId,wareId:data.studentWare.wareId});
            return;
        }else{
            angular.forEach(data.course.wares,function(data,index){
                if(data.id==wareId){
                    $scope.wareData=data;
                    $scope.wareSource=data.ware.sourceHD;
                }
            })
        }
        //获取课程名称
        $scope.noteData=data.notes;
        $scope.first=true;
        //获取当前课件评分
        if(data.studentStar){
            $scope.wareRate=data.studentStar.star;
        }else{
            $scope.wareRate=0;
        }
        var initOption={
            techOrder:["html5","flash"]
        }
        //播放器初始化
        $scope.myPlayer = neplayer("my-video",initOption,function(){
            $scope.myPlayer.setDataSource({
                type: "video/mp4",
                src:$scope.wareSource
            });
            $scope.myPlayer.seekTo($scope.data.studentWare.playedTime*1000)
            $scope.myPlayer.onPlayState(1,function(){
                if($scope.first){
                    $http({
                        url:$scope.host+"/Course/wareStartPlay/"+wareId,
                        method:"get",
                    }).success(function(data){
                         timer=$interval(function(){
                                var time=parseInt($scope.myPlayer.getCurrentPosition()/1000);
                                $http({
                                    url:$scope.host+"/Course/warePlay/"+wareId+"/"+time,
                                    method:"get",
                                }).success(function(data){
                                    if(data=="invliad"){
                                        toast("当前状态不允许快进视频进度!")
                                        $scope.myPlayer.seekTo( $scope.usefulTime*1000)    
                                    }
                                     $scope.usefulTime=data;
                                })
                            },1000)
                            $scope.first=false;
                    })
                }
            });
            $scope.myPlayer.onPlayState(2,function(){
                $interval.cancel(timer);
               service.get($scope.host+"/Course/wareStopPlay/"+wareId,'',function(data){})
                $scope.first=true;
            });
            $scope.myPlayer.onPlayState(3,function(){
                $interval.cancel(timer);
                console.log("停止");
                 $scope.first=true;
                 service.get($scope.host+"/Course/wareStopPlay/"+wareId)
            });  
        });
        $scope.allQuestionData=data.questions;
        $scope.creamQuestionData=data.creamQuestions;
        $scope.myQuestionData=data.myQuestions;
        console.log($scope.myQuestionData);
        $scope.maxSize = 4;
        $scope.totalItems=data.recordCount;
    })
   var initOption={};
    //播放器初始化方法
    
    //点击课件列表 切换播放器资源
    $scope.wareChange=function(wareId,index){
        console.log(11);
       $state.go("course.wareView",{courseId:courseId,wareId:wareId})
        }
    //从播放器页面路由到别的页面时执行方法
    $scope.$on("$stateChangeStart",function(){
        console.log($scope.myPlayer)
        //释放播放资源
        if($scope.myPlayer!=undefined){
            $interval.cancel(timer);
            $scope.myPlayer.release();
        }
        //离开页面时给服务器发送请求
        service.get($scope.host+"/Course/wareLeave/"+wareId,'',function(data){
            $scope.data=data;
        })
    })
    //切换标签页
    $scope.tabStatus=0;
    $scope.activeStatus=3;
    $scope.questionType=0;
    $scope.changeTab=function(i){
        $scope.tabStatus=i;
        $scope.activeStatus=i;
        if(i==3){
            $scope.questionType=0;
        }else if(i==4){
            $scope.questionType=1;
        }else if(i==5){
            $scope.questionType=2;
        }
        service.get($scope.host+"/Course/loadQuestion/"+courseId+"/"+$scope.questionType+"/1",'',function(data){
            console.log(data);
            if($scope.questionType==0){
                $scope.allQuestionData=data.questions;

            }else if($scope.questionType==1){
                $scope.creamQuestionData=data.questions;
            }else if($scope.questionType==2){
                $scope.myQuestionData=data.questions;
            }
             $scope.maxSize = 4;
            $scope.totalItems=data.recordCount;
        })
    }
    $scope.pageChange=function(){
        service.get($scope.host+"/Course/loadQuestion/"+courseId+"/"+$scope.questionType+"/"+$scope.currentPage,'',function(data){
            console.log(data);
            if($scope.questionType==0){
                $scope.allQuestionData=data.questions;
            }else if($scope.questionType==1){
                $scope.creamQuestionData=data.questions;
            }else if($scope.questionType==2){
                $scope.myQuestionData=data.questions;
            }
             $scope.maxSize = 4;
            $scope.totalItems=data.recordCount;
        })
    }
    //上传笔记
    $scope.postNote=function(){
        $scope.currentTime=parseInt($scope.myPlayer.getCurrentPosition()/1000);
        if(!$scope.noteText){
            toast("笔记不能为空！")
            return false;
        }
        
        service.get($scope.host+"/Course/wareNote/"+wareId+"/"+$scope.noteText+"/"+$scope.currentTime,'',function(data){
            $scope.noteData.unshift(data);
            $scope.noteText="";
        })
    }
    //课件评分
    $scope.star=function(i){
        if($scope.data.studentStar==null){
            $scope.starCount=i;
            console.log(i)
            service.get($scope.host+"/Course/wareStar/"+wareId+"/"+$scope.starCount,'',function(data){
                console.log(data);
                $scope.wareRate=data;
                // $scope.wareRate=$scope.data.course.wares[$scope.currentPlayed].star;
                toast("课件评分成功！")
            })
        }  
    }
    //提交问题
    $scope.postQuestion=function(){
        var error=$scope.questionForm.title.$error;
        if($scope.questionForm.title.$error.required){
           toast("问题标题不能为空！")
            return;
        }
        if($scope.questionForm.content.$error.required){
           toast("问题内容不能为空！")
            return;
        }
        service.post("#addQuestion",$scope.host+"/Course/questionAdd",function(data){
            $scope.questionTitle="";
            $scope.questionContent="";
            toast(data.msg);
        })
    }
}])
app.controller("courseLive",['$scope','$location','$state','service',function($scope,$location,$state,service,$stateParams){
    service.get($scope.host+"/Live/index",'',function(data){
         $scope.data=data;
    })
}])
app.controller("liveRoom",['$scope','$location','$state','service','$sce','$interval','$http','toast',function($scope,$location,$state,service,$sce,$interval,$http,toast){
    var liveId=$state.params.id;
    console.log(liveId);
    service.get($scope.host+"/Live/room/"+liveId,'',function(data){
        $scope.data=data;
         var initOptions = {
            preload: "auto",
            height: 500,
        }
         //直播初始化
        $scope.myPlayer = neplayer("my-video", initOptions, function () {
            $scope.myPlayer.setDataSource($scope.data.data.source);
            $scope.myPlayer.play();
            $scope.myPlayer.onPlayState(1, function () {
                
            });
            $scope.myPlayer.onPlayState(2, function () {
                //   clearInterval(timer);
            });
            $scope.myPlayer.onPlayState(3, function () {
                //     clearInterval(timer);
            });
        });
        //进入直播间
        service.get($scope.host+"/LiveRoom/enter/"+liveId+"/"+data.user.id+"/"+data.user.name+"/"+data.user.sex,'',function(data){
            $scope.users=data.users;
            $scope.chatContent=data.contents;
        })
        //用户聊天
        $scope.chat=function(){
            if(!$scope.chattingContent){
                toast("聊天内容不能为空！");
                return;
            }
            $http({
                url:$scope.host+"/LiveRoom/chat/"+liveId+"/"+data.user.id+"/"+$scope.chattingContent,
                method:"get",
            })
        }
         //用户退出直播间
        $scope.$on("$stateChangeStart",function(){
            $scope.myPlayer.release();
            service.get($scope.host+"/LiveRoom/quit/"+liveId+"/"+$scope.viewBag.user.id,'',function(data){});
            $interval.cancel(timer);
        })
    })
    //进入直播间
  
    //定时器获取消息
    var timer=$interval(function(){
        $http({
            url:$scope.host+"/LiveRoom/notify/"+liveId+"/"+$scope.viewBag.user.id,
            method:"get",
        }). success(function(datas){
            if (!datas)
                return; 
            if (datas.code == 0)
                return console.log(datas.content);
            console.log(1111); 
            for(var i in datas) {
               var data = JSON.parse(datas[i]);
                console.log(data);
                    if (data.action == "onChat") {
                        $("#chatContent").append("<li>"+data.data+"</li>")
                    }
                    if (data.action == "onEnter") {
                        $scope.users.push(data.data); 
                        html = "<div>" + data.data.name + "进入了直播间...</div>";
                        $("#chatContent").append("<li>"+html+"</li>");
                    }
                    if (data.action == "onQuit") {
                        for (var i = 0; i < $scope.users.length; i++)
                            if ($scope.users[i].id == data.data.id) {
                                $scope.users.splice(i, 1);
                                break;
                            } 
                        if (data.data.id.toString() == $("#targetId").val())
                            delTarget();
                        html = "<div>" + data.data.name + "离开了直播间...</div>";
                        $("#chatContent").append("<li>"+html+"</li>");
                    }
                }
        })
    },1000)
    $scope.tabStatus=0;
    $scope.changeTab=function(i){
        $scope.tabStatus=i;
    }
    $scope.raw = function (str) {
        return $sce.trustAsHtml(str);
    }
}])
app.controller("coursework",['$scope','$location','$state','service','toast',function($scope,$location,$state,service,toast){
    service.get($scope.host+"/Course/work",'',function(data){
          $scope.data=data;
    })
    $scope.showTerm=function(i){
          $scope.data.currTerm=i;
    }
    $scope.state=function(params){
        window.location.href = "#/course/courseworkDetail/" + params;
    }
}])
app.controller("courseworkDetail",['$scope','$location','$state','service','toast',function($scope,$location,$state,service,toast){
    var workId=$state.params.id;
    $scope.exerciseLength=0;
    service.get($scope.host+'/Course/workPaper/'+workId,'',function(data){
        $scope.data=data;
        angular.forEach(data.types,function(item){
            $scope.exerciseLength+=item.exerciseList.length;
        })
        console.log($scope.exerciseLength);
   },function(data){
         toast("系统暂时出错，请稍后再试！")
   })
   
    $scope.formSubmit=function(){
        var arr=$("form").serializeArray();
        console.log(arr);
        angular.forEach(arr,function(data){         
           if(!data.value){
               console.log(data);
               $scope.empty=true;
           }
        })
       if(arr.length<$scope.exerciseLength||$scope.empty==true){
           console.log(arr);
           console.log(arr.length);
           console.log($scope.exerciseLength);
          var result=confirm("作业有题目未完成，是否继续提交？");
          if(!result){
              return;
          }
       }
    //    return;
       service.post("form",$scope.host+"/Course/workPost/"+workId,function(data){
                   if(data.code==0){
                        toast(data.msg);
                        // $state.go("course.coursework");
                    }else{
                        toast(data.msg)
                    }
              })  
    }
}])
app.controller("question",['$scope','$location','$state','service','$rootScope',function($scope,$location,$state,service,$rootScope){
    // service.get($scope.host+'/Course/question')
    // .success(function(data){
    //     console.log(data);
    //     $scope.data=data;
              
    // })
}])
app.controller("allQuestion",['$scope','$location','$state','service',function($scope,$location,$state,service){
    service.get($scope.host+'/Course/question','',function(data){
        console.log(data);
       $scope.data=data;
       $scope.questionData=data.questions;
       $scope.num=0;
       $scope.maxSize = 4;
       $scope.totalItems = data.recordCount;
       $scope.courseId=data.courseId;

    })
     $scope.showTerm=function(termId,index){
          $scope.data.currTerm=termId;
         $scope.courseId=$scope.data.terms[index].scores[0].courseId;
         service.get($scope.host+"/Course/question/"+$scope.courseId,'',function(data){
             $scope.courseId=data.courseId
             data.currTerm=termId;
             $scope.data=data;
             $scope.questionData=data.questions;
             $scope.num=0;
            $scope.maxSize = 4;
            $scope.totalItems = data.recordCount;
         })
    }
    $scope.courseSelect=function(courseId,termId,num){
        $scope.courseId=courseId;
        console.log("termId"+termId)
            service.get($scope.host+"/Course/question/"+$scope.courseId,'',function(data){
                data.currTerm=termId;
                $scope.data=data;
                $scope.questionData=data.questions;
                $scope.maxSize = 4;
                $scope.totalItems = data.recordCount;
            })
            $scope.num=num;
    }
    $scope.pageChange=function(){
        service.get($scope.host+"/Course/loadQuestion/"+$scope.courseId+"/0/"+$scope.currentPage,'',function(data){
            console.log(data);
            $scope.questionData=data.questions;
        })
    }
}])

app.controller("creamQuestion",['$scope','$location','$state','service',function($scope,$location,$state,service){
    service.get($scope.host+'/Course/question','',function(data){
        $scope.data=data;
        $scope.courseId=data.courseId;
        service.get($scope.host+'/Course/loadQuestion/'+$scope.courseId+"/1/"+1,'',function(data){
            console.log(data);
            $scope.questionData=data.questions;
            $scope.num=0;
            $scope.maxSize = 4;
            $scope.totalItems=data.recordCount;
        }) 
    })
     $scope.showTerm=function(termId,index){
         console.log(termId);
          $scope.data.currTerm=termId;
         $scope.courseId=$scope.data.terms[index].scores[0].courseId;
         service.get($scope.host+"/Course/loadQuestion/"+$scope.courseId+"/1/"+1,'',function(data){
             console.log(data);
            data.currTerm=termId;
            if(data.questions.length==0){
                $scope.empty=true;
            }else{
                $scope.empty=false;
            }
            $scope.questionData=data.questions;
            $scope.num=0;
            $scope.maxSize = 4;
            $scope.totalItems = data.recordCount;
         })
    }
    $scope.courseSelect=function(courseId,termId,num){
        $scope.courseId=courseId;
        service.get($scope.host+"/Course/loadQuestion/"+$scope.courseId+"/1/"+1,'',function(data){
                data.currTerm=termId;
                if(data.questions.length==0){
                    $scope.empty=true;
                }else{
                    $scope.empty=false;
                }
                $scope.questionData=data.questions;
                $scope.maxSize = 4;
                $scope.totalItems = data.recordCount;
        })
            $scope.num=num;
    }
    $scope.pageChange=function(){
        service.get($scope.host+"/Course/loadQuestion/"+$scope.courseId+"/1/"+$scope.currentPage,'',function(data){
            $scope.questionData=data.questions;
        })
    }
}])
app.controller("myQuestion",['$scope','$location','$state','service',function($scope,$location,$state,service){
    service.get($scope.host+'/Course/question','',function(data){
         $scope.data=data;
         console.log(data);
         $scope.courseId=data.courseId;
         service.get($scope.host+'/Course/loadQuestion/'+$scope.courseId+"/2/"+1,'',function(data){
            console.log(data);
            if(data.recordCount==0){
                $scope.empty=true;
            }else{
                $scope.empty=false;
            }
            $scope.questionData=data.questions;
            $scope.num=0;
            $scope.maxSize = 4;
            $scope.totalItems=data.recordCount;
        })
       $scope.num=0;
    })
     $scope.showTerm=function(termId,index){
          $scope.data.currTerm=termId;
          $scope.courseId=$scope.data.terms[index].scores[0].courseId;
         service.get($scope.host+"/Course/loadQuestion/"+$scope.courseId+"/2/1",'',function(data){
             console.log(data);
             if(data.recordCount==0){
                $scope.empty=true;
            }else{
                $scope.empty=false;
            }
            data.currTerm=termId;
            $scope.questionData=data.questions;
            $scope.num=0;
            $scope.maxSize = 4;
            $scope.totalItems=data.recordCount;
         })
        }
    $scope.courseSelect=function(courseId,termId,num){
        $scope.courseId=courseId
        service.get($scope.host+"/Course/loadQuestion/"+$scope.courseId+"/2/1",'',function(data){
            if(data.recordCount==0){
                $scope.empty=true;
            }else{
                $scope.empty=false;
            }
            data.currTerm=termId;
            $scope.questionData=data.questions;
            $scope.maxSize = 4;
            $scope.totalItems=data.recordCount;
        })
        $scope.num=num;
    }
    $scope.pageChange=function(){
        service.get($scope.host+"/Course/loadQuestion/"+$scope.courseId+"/2/"+$scope.currentPage,'',function(data){
            $scope.questionData=data.questions;
        })
    }
}])
app.controller("questionDetail",['$scope','$state','service',function($scope,$state,service){
    console.log($state.params);
    service.get($scope.host+"/Course/questionDetail/"+$state.params.id,'',function(data){
        console.log(data);
        $scope.data=data;
    })
    $scope.postForm=function(){
        service.post("form",$scope.host+"/Course/answer/"+$state.params.id,function(data){ 
            $scope.data.answers=data;
            $scope.$apply();     
        })
    }
}])
app.controller("askQuestion",['$scope','$location','$state','service','toast',function($scope,$location,$state,service,toast){
    service.get($scope.host+'/Course/question','',function(data){
        $scope.data=data;
        $scope.courses=[];
        angular.forEach(data.terms,function(term){
            angular.forEach(term.scores,function(course){
                $scope.courses.push(course);
            })
        }) 
    })   
    $scope.formSubmit=function(){
        if($scope.addQuestion.courseId.$error.required){
            toast("请选择课程！");
            return;
        }
        if($scope.addQuestion.title.$error.required){
            toast("问题标题不能为空，请重新编辑！");
            return;
        }
        if($scope.addQuestion.content.$error.required){
            toast("问题内容不能为空，请重新编辑！");
            return;
        }
        service.post("form",$scope.host+"/Course/questionAdd",function(data){
            if(data.code==0){
                toast(data.msg)
            }else{
                toast(data.msg)
            } 
        })  
    }
}])
// app.controller("questionDetail",['$scope','$location','$state',function($scope,$location,$state,$stateParams){
//     console.log($state.params);
// }])
app.controller("examPrepare",['$scope','$location','$state','service',function($scope,$location,$state,service){
   service.get($scope.host+"/course/examCoach",'',function(data){
       $scope.data=data;
        $scope.paperData={};
        $scope.activeModule=1;
        console.log($scope.data);
        angular.forEach($scope.data.terms,function(term,index){
            if(term.id==$scope.data.currTerm){
                $scope.paperData=term.scores[0].paper;
                //获取初始时当前学期的index;
                $scope.currTermIndex=index;
                console.log(index);
                $scope.courseId=term.scores[0].courseId;
            }
        })
   })
    //初始时，课程激活未下标为0的课程
    $scope.num=0;
    //选择学期
    $scope.showRightAnswer=false;
    $scope.showTerm=function(termId,index){
        //termId为学期id，index为点击的学习的下标
        $scope.data.currTerm=termId;
        $scope.num=0;
        $scope.currTermIndex=index;
        $scope.paperData=$scope.data.terms[index].scores[0].paper;
        $scope.showRightAnswer=false;
    }
    //选择课程
    $scope.courseSelect=function(num,courseId){
        $scope.num=num;
       $scope.paperData=$scope.data.terms[$scope.currTermIndex].scores[num].paper;
       console.log($scope.paperData);
       $scope.showRightAnswer==false;
       $scope.courseId=courseId;
    }
    $scope.showRight=function(){
        console.log($scope.showRightAnswer);
        $scope.showRightAnswer=!$scope.showRightAnswer;
         console.log($scope.showRightAnswer);
    }
    //错题集
    $scope.exerciseWrong=function(i){
        console.log($scope.courseId);
        $scope.activeModule=i;
        service.get($scope.host+"/Course/examWrong",'',function(data){
            $scope.data=data;
            $scope.paperData={};
            
            console.log($scope.data);
            angular.forEach($scope.data.terms,function(term,index){
                if(term.id==$scope.data.currTerm){
                    $scope.paperData=term.scores[0].paper;
                    //获取初始时当前学期的index;
                    $scope.currTermIndex=index;
                    console.log(index);
                    $scope.courseId=term.scores[0].courseId;
                }
            })
        })
    }
    //模拟试卷
    $scope.stimulaterExam=function(i){
        $scope.activeModule=i;
        service.get($scope.host+"/course/examCoach",'',function(data){
            $scope.data=data;
            $scope.paperData={};
            
            console.log($scope.data);
            angular.forEach($scope.data.terms,function(term,index){
                if(term.id==$scope.data.currTerm){
                    $scope.paperData=term.scores[0].paper;
                    //获取初始时当前学期的index;
                    $scope.currTermIndex=index;
                    console.log(index);
                    $scope.courseId=term.scores[0].courseId;
                }
            })
        })
    }
}])
app.controller("pay",['$scope','$location','$state','service',function($scope,$location,$state,service){
     $scope.items=
        [{"item":"缴费信息","url":"pay.paymentInfo","branch":"paymentInfo"},
        {"item":"缴费明细","url":"pay.paymentSheet","branch":"paymentSheet"},]
    
}])
app.controller("paymentInfo",['$scope','$location','$state','service',function($scope,$location,$state,service){
    service.get($scope.host+"/Pay/index",'',function(data){
        $scope.data=data.pay;
        console.log($scope.data);
    })
}])
app.controller("paymentSheet",['$scope','$location','$state','service',function($scope,$location,$state,service){
    service.get($scope.host+"/Pay/index",'',function(data){
        $scope.data=data.payInfos;
    }) 
}])
app.controller("thesis",['$scope','$state','service',function($scope,$state,service){
    // service.get($scope.host+"/Thesis/index").success(function(data){
    //     console.log(data);
    //     $scope.data=data;
    // })
    console.log($scope.navStatus);
}])
app.controller("topicSelect",['$scope','$state','service','toast',function($scope,$state,service,toast){
    service.get($scope.host+"/Thesis/index",'',function(data){
         $scope.data=data;
        $scope.topics=data.datas;
    })
    $scope.select=function(){
       console.log($scope.topics.selected);
    }
    $scope.start=function(){
        service.get($scope.host+'/Thesis/buy','',function(data){
             if(data.code==0){
                toast(data.msg)
                $state.reload();
            }else{
                toast(data.msg)
            }
        })
      
    }
    $scope.postForm=function(){
        service.post("form",$scope.host+"/Thesis/select",function(data){
            if(data.code==0){
                    $state.reload();
                }else{
                    toast(data.msg)
                }
                if(data.terms){
                    $scope.data.terms=data.terms; 
                    $scope.$apply();
                }
        })
    }
}])
app.controller("thesisOutline",['$scope','$state','service','toast',function($scope,$state,service,toast){
    service.get($scope.host+"/Thesis/outline",'',function(data){
        $scope.data=data;
    })
    $scope.selectFile=function(e){
       var el=$(e.target).next();
       el.click()
    }
    $scope.fileName="（仅支持.doc与.docx格式文件）";
    function fileChange(){
        console.log(1111);
    }
    $scope.fileNameChanged=function(e){
        console.log(e.files);
        $scope.fileName=e.files[0].name;
        $scope.$apply();
    }
    $scope.postForm=function(){
        service.post("form",$scope.host+"/Thesis/uploadOutline",function(data){
            if(data.code==0){
                    toast(data.msg);
                    $state.reload();
                }else{
                    toast(data.msg)
                }
                if(data.terms){
                    $scope.data.terms=data.terms; 
                    $scope.$apply();
                }
        })
    }
}])
app.controller("firstDraft",['$scope','$state','service','toast',function($scope,$state,service,toast){
    service.get($scope.host+"/Thesis/thesis",'',function(data){
        $scope.data=data;
    })
    $scope.selectFile=function(e){
       var el=$(e.target).next();
       el.click()
    }
    $scope.fileName="（仅支持.doc与.docx格式文件）";
    function fileChange(){
        console.log(1111);
    }
    $scope.fileNameChanged=function(e){
        console.log(e.files);
        $scope.fileName=e.files[0].name;
        $scope.$apply();
    }
    $scope.postForm=function(){
        service.post("form",$scope.host+"/Thesis/uploadThesis",function(data){
            if(data.code==0){
                    toast(data.msg);
                    $state.reload();
                }else{
                    toast(data.msg)
                }
                if(data.terms){
                    $scope.data.terms=data.terms; 
                    $scope.$apply();
                }
        })
    }
}])
app.controller("finalDraft",['$scope','$state','service',function($scope,$state,service){
    service.get($scope.host+"/Thesis/thesisFinal",'',function(data){
        $scope.data=data;
    })
}])
app.controller("thesisDefense",['$scope','$state','service',function($scope,$state,service){
    service.get($scope.host+"/Thesis/reply",'',function(data){
        $scope.data=data;
    })
    $scope.defenseApply=function(){
        service.get($scope.host+"/Thesis/replyRequest",'',function(data){
            $state.reload();
        })
    }
}])
app.controller("degreeApply",['$scope','$state','service',function($scope,$state,service){
    service.get($scope.host+"/Thesis/degree",'',function(data){
        $scope.data=data;
    })
}])
app.controller("counselor",['$scope','$state','service',function($scope,$state,service){
    service.get($scope.host+"/Thesis/teacher",'',function(data){
        console.log(data);
         $scope.data=data;
    })
}])
app.controller("library",['$scope','$state','service','toast',function($scope,$state,service,toast){
  $scope.time=new Date().getTime();
  console.log($scope.time);
  $scope.starttime ='2012-12-25 20:17:24';
//   $scope.starttime = $scope.starttime.replace(new RegExp("-","gm"),"/");
//    $scope.starttimeHaoMiao = (new Date($scope.starttime)).getTime();
//    console.log($scope.starttimeHaoMiao);
//   var date=$scope.time.toLocaleString().split(" ")[0];
//   var time=$scope.time.toLocaleString().split(" ")[1]
//   var arr1=date.split("/");
//   var arr2=time.split(":")
//   console.log(arr2);
//   var str="";
   
//   for(var i=0;i<arr1.length;i++){
//       if(arr1[i].length<2){
//           arr1[i]=0+arr1[i];
//       }
//     }
//   str+=arr1.join("-");    
//   console.log(str);
}])
app.controller("examTicket",['$scope','$state','service',function($scope,$state,service){
  service.get($scope.host+"/plan/examTicket",'',function(data){
      $scope.ViewBag = data;
  })
}])
app.controller("examOnline",['$scope','$state','service','toast',function($scope,$state,service,toast){
    var id=$state.params.id;
    service.get($scope.host+"/Plan/examPaper/"+id,'',function(data){
        //console.log(data);
        $scope.data=data;
        },function(data){
            toast("系统发生未知错误，请联系管理员或者班主任",function(){
                history.back();
            });   
        })
}])
app.controller("footerDetail",['$scope','$state','service','$interval',function($scope,$state,service,$interval){
        $scope.$watch('viewBag',function(newV,oldV){
            $scope.data=newV.footers;
            //console.log($scope.data)       
            $scope.subDatas=[];
            angular.forEach($scope.data,function(footer){
                angular.forEach(footer.subDatas,function(subData){
                   //console.log(subData);
                    $scope.subDatas.push(subData);
                })
            })          
        })
        $scope.activeId=$state.params.id;
        //console.log($scope.activeId);
        service.get($scope.host+"/Article/index/"+$state.params.id,'',function(data){
            //console.log(data);
            $scope.content=data.article;
        })
}])

